import {
  IsOptional,
  IsEnum,
  IsBoolean,
  IsString,
  IsNumber,
  Min,
  Max,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';

import { ShortStatus } from '@prisma/client';

export enum SortBy {
  LATEST = 'latest',
  POPULAR = 'popular',
  RATING = 'rating',
  VIEW_COUNT = 'viewCount',
  LIKE_COUNT = 'likeCount',
}

export class ShortsListQueryDto {
  @ApiProperty({ description: '页码', example: 1, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiProperty({ description: '每页大小', example: 20, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  @Min(1)
  @Max(100)
  pageSize?: number = 20;

  @ApiProperty({ description: '分类ID', required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  categoryId?: number;

  @ApiProperty({ description: '方向ID', required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  directionId?: number;

  @ApiProperty({ description: '短剧状态', enum: ShortStatus, required: false })
  @IsOptional()
  @IsEnum(ShortStatus)
  status?: ShortStatus;

  @ApiProperty({ description: '是否免费', required: false })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean()
  isFree?: boolean;

  @ApiProperty({ description: '是否置顶', required: false })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean()
  isTop?: boolean;

  @ApiProperty({ description: '搜索关键词', required: false })
  @IsOptional()
  @IsString()
  keyword?: string;

  @ApiProperty({ description: '排序方式', enum: SortBy, required: false })
  @IsOptional()
  @IsEnum(SortBy)
  sortBy?: SortBy = SortBy.LATEST;
}

export class ChapterInfoDto {
  @ApiProperty({ description: '章节ID' })
  id: number;

  @ApiProperty({ description: '章节标题' })
  title: string;

  @ApiProperty({ description: '章节描述', required: false })
  description?: string;

  @ApiProperty({ description: '视频URL', required: false })
  videoUrl?: string;

  @ApiProperty({ description: '封面URL', required: false })
  coverUrl?: string;

  @ApiProperty({ description: '时长（分钟）', required: false })
  duration?: number;

  @ApiProperty({ description: '所需积分' })
  points: number;

  @ApiProperty({ description: '观看次数' })
  viewCount: number;

  @ApiProperty({ description: '排序' })
  sort: number;
}

export class ShortInfoDto {
  @ApiProperty({ description: '短剧ID' })
  id: number;

  @ApiProperty({ description: '短剧标题' })
  title: string;

  @ApiProperty({ description: '封面图片URL' })
  coverUrl: string;

  @ApiProperty({ description: '短剧简介', required: false })
  summary?: string;

  @ApiProperty({ description: '导演姓名' })
  instructor: string;

  @ApiProperty({ description: '观看次数' })
  viewCount: number;

  @ApiProperty({ description: '点赞数量' })
  likeCount: number;

  @ApiProperty({ description: '收藏数量' })
  favoriteCount: number;

  @ApiProperty({ description: '分享数量' })
  shareCount: number;

  @ApiProperty({ description: '是否置顶' })
  isTop: boolean;

  @ApiProperty({ description: '是否免费' })
  isFree: boolean;

  @ApiProperty({ description: '短剧状态' })
  status: ShortStatus;

  @ApiProperty({ description: '章节总数' })
  episodeCount: number;

  @ApiProperty({ description: '总时长（分钟）' })
  totalDuration: number;

  @ApiProperty({ description: '标签数组' })
  tags: string[];

  @ApiProperty({ description: '分类信息' })
  category: {
    id: number;
    name: string;
  };

  @ApiProperty({ description: '方向信息' })
  direction: {
    id: number;
    name: string;
  };

  @ApiProperty({ description: '章节列表' })
  chapters?: ChapterInfoDto[];

  @ApiProperty({ description: '创建时间' })
  createdAt: string;

  @ApiProperty({ description: '更新时间' })
  updatedAt: string;
}

export class ShortsListResponseDto {
  @ApiProperty({ description: '短剧列表' })
  list: ShortInfoDto[];

  @ApiProperty({ description: '分页信息' })
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

// 用于Swagger文档的完整API响应类型
export class ApiShortsListResponseDto {
  @ApiProperty({ description: '响应状态码', example: 200 })
  code: number;

  @ApiProperty({ description: '响应消息', example: 'success' })
  message: string;

  @ApiProperty({ description: '响应数据' })
  data: ShortsListResponseDto;

  @ApiProperty({ description: '时间戳' })
  timestamp: number;

  @ApiProperty({ description: '请求ID' })
  requestId: string;
}
